<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Service;
use App\Models\ServiceContent;

class ServiceContentController extends Controller
{
    
    public function index(Request $request)
    {
        $where = [];
        if($request->service_id) {
            $where['service_id'] = (int)$request->service_id;    
        }       
        $list = ServiceContent::where($where)
                                ->orderBy('weight','asc')
                                ->paginate(10);   
            
        return view('admin.service-content.index',compact('list','where'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $services= Service::get();
        return view('admin.service-content.add',compact('services'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {   
        $data = $request->except(['_token']);
        $flag = ServiceContent::create($data);
        if($flag) {
            return redirect('zadmin/service-content');  
        }
        $rs['status'] = false;
        $rs['msg'] = '操作失败';
        return back()->with('rs',$rs);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {   
        $services = Service::get();
        $data     = ServiceContent::find($id);
        return view('admin.service-content.edit',compact('data','services'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $rs['status'] = false;
        $rs['msg'] = '操作失败';
        $data = $request->except(['_token','_method']);
        $flag = ServiceContent::where(['id'=>$id])->update($data);
        if($flag) {
            $rs['msg'] = '操作成功';
            return redirect('zadmin/service-content')->with('rs',$rs);    
        } 
        return back();
        
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        ServiceContent::destroy($id);
        return back();
    }
}
